package com.zhiwang.planet.ui.jsmodule

import android.content.Intent
import android.net.Uri
import android.webkit.JavascriptInterface
import com.planet.processor.JsModule
import com.zhiwang.planet.event.TabChangeEvent
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.MainActivity
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.entertain.MiniGameActivity
import com.zhiwang.planet.ui.guessboard.RankActivity
import com.zhiwang.planet.ui.my.BindPayPasswordActivity
import com.zhiwang.planet.ui.my.BonusTransformActivity
import com.zhiwang.planet.ui.my.ModifyPasswordActivity
import com.zhiwang.planet.ui.my.WithDrawActivity2
import com.zhiwang.planet.ui.my.reward.RewardListActivity
import com.zhiwang.planet.util.Store
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.quickStartActivity
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.fragment_vip.*
import org.greenrobot.eventbus.EventBus


@JsModule
class NavJsModule constructor(context: BaseActivity) : BaseJsModule(context) {


    @JavascriptInterface
    fun vip() {
        WebViewActivity.loadImmersive(this, Store.URL_MEMBER_SHIP)
//        context.suicideAfterTransition()
    }

    /*去往首页*/
    @JavascriptInterface
    fun navHome() {
        context.runOnUiThread {
            EventBus.getDefault().post(TabChangeEvent(0))
//            context.suicideAfterTransition()
        }

    }

    /* 星球秘籍*/
    @JavascriptInterface
    fun navTips() {
        context.runOnUiThread {
            WebViewActivity.load(this, Store.URL_TIPS)
        }
    }

    /**
     * 去往赚钱(游戏试玩、赚钱)
     */
    @JavascriptInterface
    fun navMoneyMaking() {

        context.runOnUiThread {
            EventBus.getDefault().post(TabChangeEvent(1))
        }
        context.suicideAfterTransition()
    }

    @Deprecated("新版 tab 3 已经是vip页面")
    @JavascriptInterface
    fun navFriendInvitation() {
        Toaster.message("正在前往赚钱1")
        context.runOnUiThread {
            EventBus.getDefault().post(TabChangeEvent(3))
        }
    }

    /**
     * 去往首页的 tab
     */
    fun goTab(index: Int) {
        context.runOnUiThread {
            EventBus.getDefault().post(TabChangeEvent(index))
            if (context !is MainActivity)//不是在首页，不需要关闭
                context.suicideAfterTransition()
        }
    }

    //跳转到游戏基地
    @JavascriptInterface
    fun navigateToGameBase() {
        Toaster.message("已不支持该方法")
//        when {
//            TokenManager.isLoginValid().not() -> LoginActivity.start(this)
//            else -> context.quickStartActivity(GameBaseActivity::class.java)
//        }
    }

    @JavascriptInterface
    fun navEntertainment() {
        context.runOnUiThread {
            EventBus.getDefault().post(TabChangeEvent(1))
//            context.suicideAfterTransition()
        }
    }


//    @JavascriptInterface
//    fun navMiniGame() {
//        context.quickStartActivity(MiniGameActivity::class.java)
//    }

    @JavascriptInterface
    fun goMyLottery() {

        context.quickStartActivity(RewardListActivity::class.java)

    }

    @JavascriptInterface
    fun openLink(url: String, isExternal: String) {
        when (isExternal) {
            "false" -> {
                /*  if (url == Store.URL_FRIEND_INVITE)
                      return openLinkWithoutTitle(url)
                  else*/ WebViewActivity.load(context, url)
            }
            "true" -> {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                startActivity(intent)
            }
        }
    }

    @JavascriptInterface
    fun openLinkWithoutTitle(url: String) {
        WebViewActivity.load(this, url, false)
    }


    @JavascriptInterface
    //福利卡
    fun goWelfareCards() {
        context.quickStartActivity(BonusTransformActivity::class.java)
    }

    @JavascriptInterface
    // 忘记交易密码
    fun goForgotPayPassword() {
        context.quickStartActivity(BindPayPasswordActivity::class.java)
    }

    @JavascriptInterface
    // 忘记密码
    fun goForgotPassword() {
        context.quickStartActivity(ModifyPasswordActivity::class.java)
    }

    @JavascriptInterface
    fun openMemberShip() {
        WebViewActivity.loadImmersive(context, Store.URL_MEMBER_SHIP)
    }

    @JavascriptInterface
    fun goRank() = context.quickStartActivity(RankActivity::class.java)


    @JavascriptInterface
    fun navWithdrawMoney() = context.quickStartActivity(WithDrawActivity2::class.java)

}